package org.xx.armory.swing.controllers;

import org.xx.armory.swing.components.DialogPane;

import javax.swing.*;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeListener;

public class DialogUIContainerPropertyChangeAdapter
        extends PaneUIContainerPropertyChangeAdapter<DialogPane, JDialog>
        implements PropertyChangeListener {
    private static final DialogLocationAdapter DIALOG_LOCATION_ADAPTER = new DialogLocationAdapter();

    /**
     * {@inheritDoc}
     */
    @Override
    public void propertyChange(
            DialogPane container,
            JDialog owner,
            String propertyName,
            Object oldValue,
            Object newValue
    ) {
        super.propertyChange(container, owner, propertyName, oldValue, newValue);

        switch (propertyName) {
            case "title":
                setOwnerTitle(owner, container.getTitle());
                break;
            case "icon":
                setOwnerIcon(owner, container.getIcon());
                break;
            case "fixedSize":
                setOwnerFixedSize(owner, container.isFixedSize());
                break;
            case "owner":
                setOwnerTitle(owner, container.getTitle());
                setOwnerIcon(owner, container.getIcon());
                setOwnerFixedSize(owner, container.isFixedSize());

                owner.pack();

                if (oldValue != null) {
                    ((JDialog) oldValue).removeComponentListener(DIALOG_LOCATION_ADAPTER);
                    ((JDialog) oldValue).removeWindowListener(DIALOG_LOCATION_ADAPTER);
                    ((JDialog) oldValue).removeWindowListener(container);
                }

                owner.addComponentListener(DIALOG_LOCATION_ADAPTER);
                owner.addWindowListener(DIALOG_LOCATION_ADAPTER);
                owner.addWindowListener(container);
                break;
            default:
                break;
        }
    }

    private void setOwnerTitle(
            JDialog owner,
            String title
    ) {
        if (owner != null) {
            owner.setTitle(title);
        }
    }

    private void setOwnerIcon(
            JDialog owner,
            Image icon
    ) {
        if (owner != null) {
            owner.setIconImage(icon);
        }
    }

    private void setOwnerFixedSize(
            JDialog owner,
            boolean fixedSize
    ) {
        if (owner != null) {
            owner.setMinimumSize(owner.getPreferredSize());

            if (fixedSize) {
                owner.setMaximumSize(owner.getPreferredSize());
            } else {
                owner.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
            }

            owner.setResizable(!fixedSize);
        }
    }

    private static class DialogLocationAdapter
            implements WindowListener, ComponentListener {
        @Override
        public void componentResized(
                ComponentEvent event
        ) {
            final JDialog dialog = (JDialog) event.getComponent();
            if (!dialog.isResizable()) {
                dialog.setLocationRelativeTo(dialog.getOwner());
            }
        }

        @Override
        public void componentMoved(
                ComponentEvent event
        ) {
        }

        @Override
        public void componentShown(
                ComponentEvent event
        ) {
        }

        @Override
        public void componentHidden(
                ComponentEvent event
        ) {
        }

        @Override
        public void windowOpened(
                WindowEvent event
        ) {
            final JDialog dialog = (JDialog) event.getComponent();
            dialog.setLocationRelativeTo(dialog.getOwner());
            //
        }

        @Override
        public void windowClosing(
                WindowEvent event
        ) {
        }

        @Override
        public void windowClosed(
                WindowEvent event
        ) {
            //
        }

        @Override
        public void windowIconified(
                WindowEvent event
        ) {
        }

        @Override
        public void windowDeiconified(
                WindowEvent event
        ) {
        }

        @Override
        public void windowActivated(
                WindowEvent event
        ) {
        }

        @Override
        public void windowDeactivated(
                WindowEvent event
        ) {
        }
    }
}
